﻿using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
using System.Globalization;
using CommonUtils;
using JTZY.Model.Pay.Dto;
using JTZY.Model.Enum;
using Infrastructure;
using JTZY.Common;


namespace WANFT.Utils
{
    public static class WANFT_Util
    {

        #region VAR测试      
        //1112 皖付通接口

        //测试账号，新用户参数
        //static string _url = "https://kaifa.zgjindoufu.com";
        //static string _merId = "202289152247162424";   //商户号
        //static string _operId = "4082";   //操作号
        //static string _termId = "45463215";         //终端号       
        //static string _key = "88888888";     

        #endregion



        public static void BarcodePay(PayInfo payreq, PayRetInfo wpri)
        {

            //payreq.z_tp_mchno = _merId;
            //payreq.z_tp_operid = _operId;
            //payreq.z_tp_termid = _termId;
            //payreq.z_tp_key = _key;
            //payreq.z_tp_bank_url = _url;

            string _payurl = payreq.z_tp_bank_url + "/api/devicepay/scan_pay";

            if (string.IsNullOrWhiteSpace(_payurl))
            {
                wpri.code = PayReturnCode.FAIL.ToString();
                wpri.msg = "银行URL为空！";
                return;
            }

            JsonSerializerSettings jsetting = new JsonSerializerSettings();
            jsetting.DefaultValueHandling = DefaultValueHandling.Ignore;
            #region 组装数据   

            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("store_id", payreq.z_tp_mchno);
            dic.Add("merchant_id", payreq.z_tp_operid);
            dic.Add("code", payreq.auth_code);
            dic.Add("total_amount", payreq.total_amount);
            dic.Add("device_id", payreq.z_tp_termid);
            dic.Add("device_type", "face_14");
            dic.Add("other_no", payreq.out_trade_no);

            var dic2 = HashUtil.AsciiDictionary(dic);
            string stringsign = HashUtil.BuildQueryString(dic2);
            stringsign = stringsign + "&key=" + payreq.z_tp_key;
            string aa = stringsign.ToString();
            string sign = CommonHelper.Md5For32(aa.ToString()).ToLower(); //md5加密

            dic.Add("sign", sign);

            #endregion

            string json = JsonConvert.SerializeObject(dic, jsetting);
            //scP.AppendLine(MU.GN() + "请求URL：" + _payurl);
            //scP.AppendLine(MU.GN() + "请求报文：" + json);
            string rst = HttpUtil.HttpReqJson(_payurl, json, 60);

            if (string.IsNullOrEmpty(rst) || rst == "")
            {
                wpri.code = PayReturnCode.FAIL.ToString();
                wpri.msg = "付款失败！支付接口没有响应信息!";
                return;
            }

            WANFTRetInfo _returnPay = JsonConvert.DeserializeObject<WANFTRetInfo>(rst.ToString());
            //scP.AppendLine(MU.GN() + "响应报文：" + JsonConvert.SerializeObject(_returnPay, jsetting));          


            if (_returnPay.return_code != "SUCCESS")
            {
                wpri.code = PayReturnCode.FAIL.ToString();
                wpri.msg = string.Format("code:{0};msg:{1}", _returnPay.return_code ?? "", _returnPay.return_msg ?? "");
                return;
            }

            if (_returnPay.result_code == "SUCCESS")
            {
                wpri.code = PayReturnCode.SUCCESS.ToString();
            }
            else if (_returnPay.result_code == "USERPAYING")
            {
                wpri.code = PayReturnCode.WAIT_BUYER_PAY.ToString();

            }
            else
            {
                wpri.code = PayReturnCode.FAIL.ToString();
                wpri.msg = string.Format("code:{0};msg:{1}", _returnPay.result_code ?? "", _returnPay.result_msg ?? "");
                return;
            }


            string ret_threeOrderNo = ""; //第三方订单号
            if (rst.Contains("out_trade_no"))
                ret_threeOrderNo = (_returnPay.out_trade_no.ToString() ?? "").ToString();

            if (!string.IsNullOrWhiteSpace(ret_threeOrderNo))
            {
                wpri.trade_no = ret_threeOrderNo;
            }

            //支付完成时间
            DateTime dtTimeEnd = DateTime.Now;
            string MerTransDate = "";
            if (rst.Contains("pay_time"))
                MerTransDate = _returnPay.pay_time.ToString();

            if (!string.IsNullOrWhiteSpace(MerTransDate))
            {
                try
                {
                    dtTimeEnd = DateTime.ParseExact(MerTransDate, "yyyyMMddHHmmss", CultureInfo.InvariantCulture);

                }
                catch { }
            }
            wpri.trade_time = dtTimeEnd.ToString("yyyy-MM-dd HH:mm:ss");

            //元：
            string ret_total_amt = "";
            if (rst.Contains("total_amount"))
                ret_total_amt = (_returnPay.total_amount.ToString() ?? "").ToString();

            if (!string.IsNullOrEmpty(ret_total_amt))
            {
                wpri.total_amount = ret_total_amt;
            }

        }

        public static void OrderQuery(PayInfo payreq, PayRetInfo wpri)
        {

            //payreq.z_tp_mchno = _merId;
            //payreq.z_tp_operid = _operId;
            //payreq.z_tp_termid = _termId;
            //payreq.z_tp_key = _key;
            //payreq.z_tp_bank_url = _url;

            string _queryurl = payreq.z_tp_bank_url + "/api/devicepay/order_query";

            JsonSerializerSettings jsetting = new JsonSerializerSettings();
            jsetting.DefaultValueHandling = DefaultValueHandling.Ignore;

            #region 组装数据  

            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("store_id", payreq.z_tp_mchno);
            dic.Add("merchant_id", payreq.z_tp_operid);
            dic.Add("device_id", payreq.z_tp_termid);
            dic.Add("device_type", "face_14");
            dic.Add("other_no", payreq.out_trade_no);

            var dic2 = HashUtil.AsciiDictionary(dic);
            string stringsign = HashUtil.BuildQueryString(dic2);
            stringsign = stringsign + "&key=" + payreq.z_tp_key;
            string aa = stringsign.ToString();
            string sign = CommonHelper.Md5For32(aa.ToString()).ToLower(); //md5加密

            dic.Add("sign", sign);

            #endregion

            string json = JsonConvert.SerializeObject(dic, jsetting);
            //scP.AppendLine(MU.GN() + "请求URL：" + _queryurl);
            //scP.AppendLine(MU.GN() + "请求报文：" + json);
            string rst = HttpUtil.HttpReqJson(_queryurl, json, 20);
            if (string.IsNullOrEmpty(rst) || rst == "")
            {
                wpri.code = PayReturnCode.FAIL.ToString();
                wpri.msg = "付款失败！支付接口没有响应信息!";
                return;
            }


            WANFTRetInfo _returnPay = JsonConvert.DeserializeObject<WANFTRetInfo>(rst.ToString());
            //scP.AppendLine(MU.GN() + "响应报文：" + JsonConvert.SerializeObject(_returnPay, jsetting));

            if (_returnPay.return_code != "SUCCESS")
            {
                wpri.code = PayReturnCode.FAIL.ToString();
                wpri.msg = string.Format("code:{0},code:{1}", _returnPay.return_code ?? "", _returnPay.return_msg ?? "");
                return;
            }

            wpri.code = PayReturnCode.SUCCESS.ToString();

            if (_returnPay.result_code == "SUCCESS")
            {

                wpri.trade_status = PayTradeStatus.TRADE_SUCCESS.ToString();
            }
            else if (_returnPay.result_code == "USERPAYING")
            {
                wpri.trade_status = PayTradeStatus.WAIT_BUYER_PAY.ToString();
            }
            else
            {
                wpri.trade_status = PayTradeStatus.TRADE_FAIL.ToString();
                wpri.msg = string.Format("code:{0};msg:{1}", _returnPay.result_code ?? "", _returnPay.result_msg ?? "");
                return;
            }


            string ret_threeOrderNo = ""; //第三方订单号
            if (rst.Contains("out_trade_no"))
                ret_threeOrderNo = (_returnPay.out_trade_no.ToString() ?? "").ToString();

            if (!string.IsNullOrWhiteSpace(ret_threeOrderNo))
            {
                wpri.trade_no = ret_threeOrderNo;
            }

            //支付完成时间
            DateTime dtTimeEnd = DateTime.Now;
            string MerTransDate = "";
            if (rst.Contains("pay_time"))
                MerTransDate = _returnPay.pay_time.ToString();

            if (!string.IsNullOrWhiteSpace(MerTransDate))
            {
                try
                {
                    dtTimeEnd = DateTime.ParseExact(MerTransDate, "yyyyMMddHHmmss", CultureInfo.InvariantCulture);
                }
                catch { }
            }
            wpri.trade_time = dtTimeEnd.ToString("yyyy-MM-dd HH:mm:ss");

            //元：
            string ret_total_amt = "";
            if (rst.Contains("total_amount"))
                ret_total_amt = _returnPay.total_amount.ToString();

            if (!string.IsNullOrEmpty(ret_total_amt))
            {
                wpri.total_amount = ret_total_amt;
            }


        }

        public static void Refund(PayInfo payreq, PayRetInfo wpri)
        {

            string _refundurl = payreq.z_tp_bank_url + "/api/devicepay/refund";

            JsonSerializerSettings jsetting = new JsonSerializerSettings();
            jsetting.DefaultValueHandling = DefaultValueHandling.Ignore;

            #region 组装数据   

            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("store_id", payreq.z_tp_mchno);
            dic.Add("merchant_id", payreq.z_tp_operid);
            dic.Add("device_id", payreq.z_tp_termid);
            dic.Add("device_type", "face_14");
            dic.Add("refund_amount", payreq.refund_amount);
            dic.Add("refund_no", payreq.out_request_no);
            dic.Add("out_trade_no", payreq.out_trade_no);


            var dic2 = HashUtil.AsciiDictionary(dic);
            string stringsign = HashUtil.BuildQueryString(dic2);
            stringsign = stringsign + "&key=" + payreq.z_tp_key;
            string aa = stringsign.ToString();
            string sign = CommonHelper.Md5For32(aa.ToString()).ToLower(); //md5加密

            dic.Add("sign", sign);
            #endregion

            string json = JsonConvert.SerializeObject(dic, jsetting);
            //scP.AppendLine(MU.GN() + "请求URL：" + _refundurl);
            //scP.AppendLine(MU.GN() + "请求报文：" + json);
            string rst = HttpUtil.HttpReqJson(_refundurl, json, 30);
            if (string.IsNullOrEmpty(rst) || rst == "")
            {
                wpri.code = PayReturnCode.FAIL.ToString();
                wpri.msg = "付款失败！支付接口没有响应信息!";
                return;
            }


            WANFTRetInfo _returnPay = JsonConvert.DeserializeObject<WANFTRetInfo>(rst.ToString());
            //scP.AppendLine(MU.GN() + "响应报文：" + JsonConvert.SerializeObject(_returnPay, jsetting));

            if (_returnPay.return_code != "SUCCESS")
            {
                wpri.code = PayReturnCode.FAIL.ToString();
                wpri.msg = string.Format("code:{0};msg:{1}", _returnPay.return_code ?? "", _returnPay.return_msg ?? "");
                return;
            }

            if (_returnPay.result_code == "SUCCESS")
            {
                wpri.code = PayReturnCode.SUCCESS.ToString();
            }
            else if (_returnPay.result_code == "USERPAYING")
            {

                #region 退款查询，轮询4次
                int li_flag = 0;
                while (true)
                {
                    PayRetInfo retInfo2 = new PayRetInfo();
                    retInfo2.out_trade_no = payreq.out_request_no;
                    RefundQuery(payreq, retInfo2);

                    li_flag++; //整个退款只有20秒，查询4次，间隔3秒，如果还没返回成功，也相当于成功

                    if (retInfo2 != null)
                    {
                        if (retInfo2.refund_status == PayRefundStatus.SUCCESS.ToString() || li_flag == 4)
                        {
                            wpri.code = PayReturnCode.SUCCESS.ToString();
                            break;
                        }
                        else if (retInfo2.refund_status == PayRefundStatus.PROCESSING.ToString())
                        {
                            System.Threading.Thread.Sleep(3000);
                        }
                        else
                        {
                            wpri.code = PayReturnCode.FAIL.ToString();
                            wpri.msg = retInfo2.msg ?? "";
                            break;
                        }
                    }
                    else
                    {
                        if (li_flag == 4)
                        {
                            wpri.code = PayReturnCode.SUCCESS.ToString();
                            break;
                        }
                    }
                }
                #endregion
            }
            else
            {
                wpri.code = PayReturnCode.FAIL.ToString();
                wpri.msg = string.Format("code:{0};msg:{1}", _returnPay.result_code ?? "", _returnPay.result_msg ?? "");
                return;
            }


            string ret_threeOrderNo = ""; //第三方订单号
            if (rst.Contains("out_trade_no"))
                ret_threeOrderNo = (_returnPay.out_trade_no.ToString() ?? "").ToString();

            if (!string.IsNullOrWhiteSpace(ret_threeOrderNo))
            {
                wpri.trade_no = ret_threeOrderNo;
            }

            DateTime dtTimeEnd = DateTime.Now;
            wpri.refund_time = dtTimeEnd.ToString("yyyy-MM-dd HH:mm:ss");

        }

        public static void RefundQuery(PayInfo payreq, PayRetInfo wpri)
        {
            //payreq.z_tp_mchno = _merId;
            //payreq.z_tp_operid = _operId;
            //payreq.z_tp_termid = _termId;
            //payreq.z_tp_key = _key;
            //payreq.z_tp_bank_url = _url;


            string _refundqueryurl = payreq.z_tp_bank_url + "/api/devicepay/refund_query";

            JsonSerializerSettings jsetting = new JsonSerializerSettings();
            jsetting.DefaultValueHandling = DefaultValueHandling.Ignore;

            #region 组装数据  

            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("store_id", payreq.z_tp_mchno);
            dic.Add("merchant_id", payreq.z_tp_operid);
            dic.Add("device_id", payreq.z_tp_termid);
            dic.Add("device_type", "face_14");
            dic.Add("refund_no", wpri.out_trade_no);


            var dic2 = HashUtil.AsciiDictionary(dic);
            string stringsign = HashUtil.BuildQueryString(dic2);
            stringsign = stringsign + "&key=" + payreq.z_tp_key;
            string aa = stringsign.ToString();
            string sign = CommonHelper.Md5For32(aa.ToString()).ToLower(); //md5加密

            dic.Add("sign", sign);
            #endregion

            string json = JsonConvert.SerializeObject(dic, jsetting);
            //scP.AppendLine(MU.GN() + "请求URL：" + _refundqueryurl);
            //scP.AppendLine(MU.GN() + "请求报文：" + json);
            string rst = HttpUtil.HttpReqJson(_refundqueryurl, json, 20);

            if (string.IsNullOrEmpty(rst) || rst == "")
            {
                wpri.code = PayReturnCode.FAIL.ToString();
                wpri.msg = "退款失败！退款接口没有响应信息!";
                return;
            }

            WANFTRetInfo _returnPay = JsonConvert.DeserializeObject<WANFTRetInfo>(rst.ToString());
            //scP.AppendLine(MU.GN() + "响应报文：" + JsonConvert.SerializeObject(_returnPay, jsetting));

            if (_returnPay.return_code != "SUCCESS")
            {
                wpri.code = PayReturnCode.FAIL.ToString();
                wpri.msg = string.Format("code:{0};msg:{1}", _returnPay.return_code ?? "", _returnPay.return_msg ?? "");
                return;
            }

            if (_returnPay.result_code == "SUCCESS")
            {
                wpri.refund_status = PayRefundStatus.SUCCESS.ToString();
            }
            else if (_returnPay.result_code == "USERPAYING")
            {
                wpri.refund_status = PayRefundStatus.PROCESSING.ToString();
            }

            else
            {
                wpri.refund_status = PayRefundStatus.FAIL.ToString();
                wpri.msg = string.Format("code:{0};msg:{1}", _returnPay.result_code ?? "", _returnPay.result_msg ?? "");
                return;
            }

            string ret_threeOrderNo = ""; //第三方订单号
            if (rst.Contains("other_no"))
                ret_threeOrderNo = (_returnPay.other_no.ToString() ?? "").ToString();

            if (!string.IsNullOrWhiteSpace(ret_threeOrderNo))
            {
                wpri.trade_no = ret_threeOrderNo;
            }

            DateTime dtTimeEnd = DateTime.Now;
            wpri.refund_time = dtTimeEnd.ToString("yyyy-MM-dd HH:mm:ss");

        }

        public static void RefundMiniQuery(PayInfo payreq, PayRetInfo wpri)
        {
            //payreq.z_tp_mchno = _merId;
            //payreq.z_tp_operid = _operId;
            //payreq.z_tp_termid = _termId;
            //payreq.z_tp_key = _key;
            //payreq.z_tp_bank_url = _url;


            string _refundqueryurl = payreq.z_tp_bank_url + "/api/devicepay/mini_refund_query";

            JsonSerializerSettings jsetting = new JsonSerializerSettings();
            jsetting.DefaultValueHandling = DefaultValueHandling.Ignore;

            #region 组装数据  

            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("store_id", payreq.z_tp_mchno);
            dic.Add("merchant_id", payreq.z_tp_operid);
            dic.Add("device_id", payreq.z_tp_termid);
            dic.Add("device_type", "face_14");
            dic.Add("refund_no", wpri.out_trade_no);


            var dic2 = HashUtil.AsciiDictionary(dic);
            string stringsign = HashUtil.BuildQueryString(dic2);
            stringsign = stringsign + "&key=" + payreq.z_tp_key;
            string aa = stringsign.ToString();
            string sign = CommonHelper.Md5For32(aa.ToString()).ToLower(); //md5加密

            dic.Add("sign", sign);
            #endregion

            string json = JsonConvert.SerializeObject(dic, jsetting);
            //scP.AppendLine(MU.GN() + "请求URL：" + _refundqueryurl);
            //scP.AppendLine(MU.GN() + "请求报文：" + json);
            string rst = HttpUtil.HttpReqJson(_refundqueryurl, json, 20);

            if (string.IsNullOrEmpty(rst) || rst == "")
            {
                wpri.code = PayReturnCode.FAIL.ToString();
                wpri.msg = "退款失败！退款接口没有响应信息!";
                return;
            }

            WANFTRetInfo _returnPay = JsonConvert.DeserializeObject<WANFTRetInfo>(rst.ToString());
            //scP.AppendLine(MU.GN() + "响应报文：" + JsonConvert.SerializeObject(_returnPay, jsetting));

            if (_returnPay.return_code != "SUCCESS")
            {
                wpri.code = PayReturnCode.FAIL.ToString();
                wpri.msg = string.Format("code:{0};msg:{1}", _returnPay.return_code ?? "", _returnPay.return_msg ?? "");
                return;
            }

            if (_returnPay.result_code == "SUCCESS")
            {
                wpri.refund_status = PayRefundStatus.SUCCESS.ToString();
            }
            else if (_returnPay.result_code == "USERPAYING")
            {
                wpri.refund_status = PayRefundStatus.PROCESSING.ToString();
            }

            else
            {
                wpri.refund_status = PayRefundStatus.FAIL.ToString();
                wpri.msg = string.Format("code:{0};msg:{1}", _returnPay.result_code ?? "", _returnPay.result_msg ?? "");
                return;
            }

            string ret_threeOrderNo = ""; //第三方订单号
            if (rst.Contains("other_no"))
                ret_threeOrderNo = (_returnPay.other_no.ToString() ?? "").ToString();

            if (!string.IsNullOrWhiteSpace(ret_threeOrderNo))
            {
                wpri.trade_no = ret_threeOrderNo;
            }

            DateTime dtTimeEnd = DateTime.Now;
            wpri.refund_time = dtTimeEnd.ToString("yyyy-MM-dd HH:mm:ss");

        }

        public static void RefundMini(PayInfo payreq, PayRetInfo wpri)
        {

            string _refundurl = payreq.z_tp_bank_url + "/api/devicepay/mini_refund";

            JsonSerializerSettings jsetting = new JsonSerializerSettings();
            jsetting.DefaultValueHandling = DefaultValueHandling.Ignore;

            #region 组装数据   

            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("store_id", payreq.z_tp_mchno);
            dic.Add("merchant_id", payreq.z_tp_operid);
            dic.Add("device_id", payreq.z_tp_termid);
            dic.Add("device_type", "face_14");
            dic.Add("refund_amount", payreq.refund_amount);
            dic.Add("refund_no", payreq.out_request_no);
            dic.Add("out_trade_no", payreq.out_trade_no);


            var dic2 = HashUtil.AsciiDictionary(dic);
            string stringsign = HashUtil.BuildQueryString(dic2);
            stringsign = stringsign + "&key=" + payreq.z_tp_key;
            string aa = stringsign.ToString();
            string sign = CommonHelper.Md5For32(aa.ToString()).ToLower(); //md5加密

            dic.Add("sign", sign);
            #endregion

            string json = JsonConvert.SerializeObject(dic, jsetting);
            //scP.AppendLine(MU.GN() + "请求URL：" + _refundurl);
            //scP.AppendLine(MU.GN() + "请求报文：" + json);
            string rst = HttpUtil.HttpReqJson(_refundurl, json, 30);
            if (string.IsNullOrEmpty(rst) || rst == "")
            {
                wpri.code = PayReturnCode.FAIL.ToString();
                wpri.msg = "付款失败！支付接口没有响应信息!";
                return;
            }


            WANFTRetInfo _returnPay = JsonConvert.DeserializeObject<WANFTRetInfo>(rst.ToString());
            //scP.AppendLine(MU.GN() + "响应报文：" + JsonConvert.SerializeObject(_returnPay, jsetting));

            if (_returnPay.return_code != "SUCCESS")
            {
                wpri.code = PayReturnCode.FAIL.ToString();
                wpri.msg = string.Format("code:{0};msg:{1}", _returnPay.return_code ?? "", _returnPay.return_msg ?? "");
                return;
            }

            if (_returnPay.result_code == "SUCCESS")
            {
                wpri.code = PayReturnCode.SUCCESS.ToString();
            }
            else if (_returnPay.result_code == "USERPAYING")
            {

                #region 退款查询，轮询4次
                int li_flag = 0;
                while (true)
                {
                    PayRetInfo retInfo2 = new PayRetInfo();
                    retInfo2.out_trade_no = payreq.out_request_no;
                    RefundMiniQuery(payreq, retInfo2);

                    li_flag++; //整个退款只有20秒，查询4次，间隔3秒，如果还没返回成功，也相当于成功

                    if (retInfo2 != null)
                    {
                        if (retInfo2.refund_status == PayRefundStatus.SUCCESS.ToString() || li_flag == 4)
                        {
                            wpri.code = PayReturnCode.SUCCESS.ToString();
                            break;
                        }
                        else if (retInfo2.refund_status == PayRefundStatus.PROCESSING.ToString())
                        {
                            System.Threading.Thread.Sleep(3000);
                        }
                        else
                        {
                            wpri.code = PayReturnCode.FAIL.ToString();
                            wpri.msg = retInfo2.msg ?? "";
                            break;
                        }
                    }
                    else
                    {
                        if (li_flag == 4)
                        {
                            wpri.code = PayReturnCode.SUCCESS.ToString();
                            break;
                        }
                    }
                }
                #endregion
            }
            else
            {
                wpri.code = PayReturnCode.FAIL.ToString();
                wpri.msg = string.Format("code:{0};msg:{1}", _returnPay.result_code ?? "", _returnPay.result_msg ?? "");
                return;
            }


            string ret_threeOrderNo = ""; //第三方订单号
            if (rst.Contains("out_trade_no"))
                ret_threeOrderNo = (_returnPay.out_trade_no.ToString() ?? "").ToString();

            if (!string.IsNullOrWhiteSpace(ret_threeOrderNo))
            {
                wpri.trade_no = ret_threeOrderNo;
            }

            DateTime dtTimeEnd = DateTime.Now;
            wpri.refund_time = dtTimeEnd.ToString("yyyy-MM-dd HH:mm:ss");

        }


        public static void JSAPIPay(PayInfo payreq, PayRetInfo wpri)
        {

            string _payurl = payreq.z_tp_bank_url + "/api/devicepay/mini_pay";

            if (string.IsNullOrWhiteSpace(_payurl))
            {
                wpri.code = PayReturnCode.FAIL.ToString();
                wpri.msg = "银行URL为空！";
                return;
            }

            JsonSerializerSettings jsetting = new JsonSerializerSettings();
            jsetting.DefaultValueHandling = DefaultValueHandling.Ignore;
            #region 组装数据   

            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("store_id", payreq.z_tp_mchno);
            dic.Add("merchant_id", payreq.z_tp_operid);
            dic.Add("total_amount", payreq.total_amount);
            dic.Add("device_id", payreq.z_tp_termid);
            dic.Add("device_type", "face_14");
            dic.Add("open_id", payreq.z_tp_opeid);
            dic.Add("other_no", payreq.out_trade_no);
            dic.Add("ways_source", "weixin");
            dic.Add("pay_type", "02");
            dic.Add("notify_url", payreq.notify_url);

            var dic2 = HashUtil.AsciiDictionary(dic);
            string stringsign = HashUtil.BuildQueryString(dic2);
            stringsign = stringsign + "&key=" + payreq.z_tp_key;
            string aa = stringsign.ToString();
            string sign = CommonHelper.Md5For32(aa.ToString()).ToLower(); //md5加密

            dic.Add("sign", sign);

            #endregion

            string json = JsonConvert.SerializeObject(dic, jsetting);
            //scP.AppendLine(MU.GN() + "请求URL：" + _payurl);
            //scP.AppendLine(MU.GN() + "请求报文：" + json);
            string rst = HttpUtil.HttpReqJson(_payurl, json, 60);

            if (string.IsNullOrEmpty(rst) || rst == "")
            {
                wpri.code = PayReturnCode.FAIL.ToString();
                wpri.msg = "付款失败！支付接口没有响应信息!";
                return;
            }

            WANFTRetInfo _returnPay = JsonConvert.DeserializeObject<WANFTRetInfo>(rst.ToString());
            //scP.AppendLine(MU.GN() + "响应报文：" + JsonConvert.SerializeObject(_returnPay, jsetting));          


            if (_returnPay.return_code != "SUCCESS")
            {
                wpri.code = PayReturnCode.FAIL.ToString();
                wpri.msg = string.Format("code:{0};msg:{1}", _returnPay.return_code ?? "", _returnPay.return_msg ?? "");
                return;
            }

            if (_returnPay.result_code == "SUCCESS")
            {
                wpri.code = PayReturnCode.SUCCESS.ToString();
                wpri.payPackage = _returnPay.payPackage;
                wpri.paySign = _returnPay.paySign;
                wpri.payAppId = _returnPay.payAppId;
                wpri.paynonceStr = _returnPay.paynonceStr;
                wpri.paySignType = _returnPay.paySignType;
                wpri.sign = _returnPay.sign;
                wpri.timeStamp = CommonHelper.ConvertDateTimeToInt( DateTime.Now).ToString();
            }
            else if (_returnPay.result_code == "USERPAYING")
            {
                wpri.code = PayReturnCode.WAIT_BUYER_PAY.ToString();

            }
            else
            {
                wpri.code = PayReturnCode.FAIL.ToString();
                wpri.msg = string.Format("code:{0};msg:{1}", _returnPay.result_code ?? "", _returnPay.result_msg ?? "");
                return;
            }


            string ret_threeOrderNo = ""; //第三方订单号
            if (rst.Contains("out_trade_no"))
                ret_threeOrderNo = (_returnPay.out_trade_no.ToString() ?? "").ToString();

            if (!string.IsNullOrWhiteSpace(ret_threeOrderNo))
            {
                wpri.trade_no = ret_threeOrderNo;
            }

            //支付完成时间
            DateTime dtTimeEnd = DateTime.Now;
            string MerTransDate = "";
            if (rst.Contains("pay_time"))
                MerTransDate = _returnPay.pay_time.ToString();

            if (!string.IsNullOrWhiteSpace(MerTransDate))
            {
                try
                {
                    dtTimeEnd = DateTime.ParseExact(MerTransDate, "yyyyMMddHHmmss", CultureInfo.InvariantCulture);

                }
                catch { }
            }
            wpri.trade_time = dtTimeEnd.ToString("yyyy-MM-dd HH:mm:ss");

            //元：
            string ret_total_amt = "";
            if (rst.Contains("total_amount"))
                ret_total_amt = (_returnPay.total_amount.ToString() ?? "").ToString();

            if (!string.IsNullOrEmpty(ret_total_amt))
            {
                wpri.total_amount = ret_total_amt;
            }

        }


    }


}


